<template>
	<view>
		<view class="accont-card">
			<text class="accont-title">注册</text>
			<text class="accont-title-sm">请填写以下信息完成注册</text>
			<view class="flex margin-top-lg">
				<view class="accont-text flex align-center">
					账号：
				</view>
				<view class="accont-input flex align-center ">
					<view class="margin-right-lg">
						<image src="../../static/images/accont/username.png" alt="" />
					</view>
					<input 
					 type="text"
					 placeholder="请输入账号"
					 v-model="username"
					/>
				</view>
			</view>
			<view class="flex margin-top-lg">
				<view class="accont-text flex align-center">
					密码：
				</view>
				<view class="accont-input flex align-center ">
					<view class="margin-right-lg">
						<image src="../../static/images/accont/password.png" alt="" />
					</view>
					<input  
					 type="password"
					 placeholder="请输入密码"
					 v-model="password"
					/>
				</view>
			</view>
		    <button class="accont-button" @click="handleRegister">注册</button>
			<view class="toRegister" @click="handleToLogin">
				已经账号？去登录
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref,type Ref } from 'vue';
import {userRegister} from '../../api/user'
import type{ ErrorType} from '../../model/user'
//注册逻辑
const username:Ref<string>=ref('')
const password:Ref<string>=ref('')

const handleRegister =async () =>{
	if(!username.value.trim()||!password.value.trim()){
		uni.showToast({
			title:"账号或密码不能为空",
			icon:'none'
		})
		return
	}
	if(username.value.trim().length>6){
		uni.showToast({
			title:"账号长度不能超过六个字",
			icon:'none'
		})
		return
	}
	if(password.value.trim().length>12){
		uni.showToast({
			title:"账号长度不能超过12个字符",
			icon:'none'
		})
		return
	}
	let title="注册成功"
	try{
		let res =await userRegister(username.value,password.value)
		console.log("测试注册",res);
		uni.navigateTo({
			url:"/pages/accont/login"
		})
	}catch(e){
		console.log("注册失败",e);
		title=(e as ErrorType).data!.error
	}
	uni.showToast({
		title,
		icon:'none'
	})
}
//跳转登录页面
const handleToLogin = () =>{
	uni.navigateTo({
		url:"/pages/accont/login"
	})
}
</script>

<style lang="scss">
page{
	background-color:#FFF9F0 ;
} 
.accont-card{
	width: 600upx;
	height: 800upx;
	position: absolute;
	top:200upx;
	left:75upx;
	display: flex;
	flex-direction: column;
	align-items: center; /* 子元素水平居中 */
	text-align: center; /* 文本居中 */
	.accont-title{
		font-size: 60rpx;
		margin-top:30upx;

		font-weight: bolder;
		color: #FD7753;
	}
	.accont-title-sm{
		color:#999;
		font-size: 30upx;
		font-weight: 500;
		margin-top: 30upx;
	}
	.accont-input{
		background-color: #fff;
		width: 500upx;
		height: 100upx;
		padding: 20upx;
		border: 1rpx solid #ccc;
		border-radius: 20rpx;
		.accont-icons{
			width: 30rpx;
		}
	}
	.accont-text{
		font-size: 30upx;
	}
	image{
		width: 40upx;
		height: 40upx;
	}
	button{
		background-color: #FD7753;
		color: #fff;
		width: 600upx;
		border-radius: 20upx;
		margin-top: 70upx;
	}
	.toRegister{
		margin-top: 50upx;
		margin-left: 440upx;
		font-size: 20upx;
		color: #FFA07A;
		border-bottom:1rpx solid #FFA07A ;
	}
}
</style>
